﻿using System;
using System.Web.Mvc;
using SmallShop.BaseWeb;
using SmallShop.Frame;

namespace SmallShop.ManagerWeb.Controllers
{
    /// <summary>
    /// 图片上传文件
    /// </summary>
    public class ImgController : SmallController
    {
        [HttpPost]
        public ActionResult Uplaod()
        {
            if (Request.Files.Count > 0 && Request.Files[0] != null)
            {
                var f = Request.Files[0];
                var names = f.FileName.Split('.');
                var ext = names[names.Length - 1];
                if (string.Equals(ext, "jpg", StringComparison.CurrentCultureIgnoreCase)
                    || string.Equals(ext, "jpeg", StringComparison.CurrentCultureIgnoreCase)
                    || string.Equals(ext, "png", StringComparison.CurrentCultureIgnoreCase))
                {
                    var imgUrl = Server.MapPath("~/Content/images");
                    var fname = Guid.NewGuid() + "." + ext;
                    var abPath = imgUrl + "/" + fname;
                     var url = "/Content/images/" + fname;
                    f.SaveAs(abPath);
                    return Json(new ResultModel {Data = url});
                }
                return Json(new ResultModel { Code = ResultModel.InValid, Message = "不是有效的图片文件" });
            }
            return Json(new ResultModel {Code = ResultModel.InValid, Message = "无效的文件"});
        }
    }
}